package com.wxj.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.dao.CommentMapper;
import com.wxj.book.pojo.Comment;
import com.wxj.book.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    /**
     * 查询评论内容
     * @param pageNum
     * @param pageSize
     * @param bookId
     * @return
     */
    @Override
    public PageInfo<Comment> getByBookId(Integer pageNum, Integer pageSize, Integer bookId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> comments = commentMapper.getByBookId(bookId);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        return pageInfo;

    }

    /**
     * 根据用户查阅记录
     * @param stuNum
     * @return
     */
    @Override
    public Comment getByStuNum(Integer stuNum) {
        return commentMapper.getByStuNum(stuNum);
    }

    /**
     * 添加评论
     * @param comment
     * @param stuNum
     * @return
     */
    @Override
    public int addComment(Comment comment, Integer stuNum) {
        if (comment.getId() == null) {
            Comment newComment = new Comment();
            newComment.setBookId(comment.getBookId());
            newComment.setContent(comment.getContent());
            newComment.setStatus(1);
            newComment.setStuNum(stuNum);
            return commentMapper.insertSelective(newComment);
        }
        Comment newComment = new Comment();
        newComment.setId(comment.getId());
        newComment.setBookId(comment.getBookId());
        newComment.setContent(comment.getContent());
        newComment.setStatus(1);
        newComment.setStuNum(stuNum);
        return commentMapper.updateByPrimaryKeySelective(newComment);
    }
}
